package com.example.ymm.modules1.controller.sys.iot;

import com.alibaba.fastjson.JSONObject;
import com.example.ymm.model.CommonResult;
import com.example.ymm.model.TableList;
import com.example.ymm.model.YmPageDto;
import com.example.ymm.modules1.controller.BaseController;
import com.example.ymm.modules1.entity.my.sys.iot.IotShelf;
import com.example.ymm.modules1.query.sys.iot.IotShelfQueryCriteria;
import com.example.ymm.modules1.service.dd.iot.IotHttpUtil;
import com.example.ymm.modules1.service.sys.iot.IotShelfService;
import com.example.ymm.modules1.service.sys.iot.IotTagService;
import com.example.ymm.util.UtilValidate;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/hl/ex/iot")
@Tag(name= "后台 亮灯管理")
@Slf4j
public class SysIotController extends BaseController {

    @Autowired
    IotHttpUtil iotHttpUtil;

    @Autowired
    IotShelfService iotShelfService;
    @Autowired
    IotTagService iotTagService;

    @Operation(summary= "部门列表")
    @RequestMapping(value = "/deptList", method = RequestMethod.POST)
    public CommonResult deptList(@RequestBody JSONObject query) {
        try {
            query.put("orderByColumn","create_time");
            query.put("isAsc","desc");
            TableList<JSONObject> list = iotHttpUtil.getList("/system/dept/list", query);
            return CommonResult.success(list);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }
    }

    @Operation(summary= "部门下拉")
    @RequestMapping(value = "/deptSelectList", method = RequestMethod.POST)
    public CommonResult deptSelectList(@RequestBody JSONObject query) {
        try {
            query.put("pageNum",1);
            query.put("pageSize",100);
            query.put("orderByColumn","create_time");
            query.put("isAsc","desc");
            TableList<JSONObject> list = iotHttpUtil.getList("/system/dept/list", query);

            return CommonResult.success(list.getList());
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }
    }

    @Operation(summary= "部门编辑")
    @RequestMapping(value = "/deptSave", method = RequestMethod.POST)
    public CommonResult deptSave(@RequestBody JSONObject object) {
        try {
            JSONObject jsonObject = iotHttpUtil.httpPut("/system/dept", object);
            return CommonResult.success(jsonObject);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }
    }



    @Operation(summary= "架子列表")
    @RequestMapping(value = "/shelfList", method = RequestMethod.POST)
    public CommonResult shelfList(@Valid IotShelfQueryCriteria criteria, YmPageDto ymPage) {
        try{
            TableList<IotShelf> list = iotShelfService.list(criteria, ymPage);
            return CommonResult.success(list,"获取成功");
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }


    @Operation(summary= "架子下拉")
    @RequestMapping(value = "/shelfSelectList", method = RequestMethod.POST)
    public CommonResult shelfSelectList(@Valid IotShelfQueryCriteria criteria) {
        try{
            List<IotShelf> list = iotShelfService.queryAll(criteria);
            return CommonResult.success(list,"获取成功");
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }


    @Operation(summary= "ms库位信息下拉")
    @RequestMapping(value = "/houseLocationSelectList", method = RequestMethod.POST)
    public CommonResult houseLocationSelectList(@Valid IotShelfQueryCriteria criteria) {
        try{
            List<IotShelf> list = iotShelfService.queryAll(criteria);
            return CommonResult.success(list,"获取成功");
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }

    @Operation(summary= "架子保存")
    @RequestMapping(value = "/shelfSave", method = RequestMethod.POST)
    public CommonResult shelfSave(IotShelf iotShelf) {
        try{
            if(UtilValidate.isEmpty(iotShelf.getShelfName())){
                return CommonResult.error("架子名称不能为空!");
            }
            iotShelfService.saveOrUpdate(iotShelf);

            return CommonResult.success();
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }

    @Operation(summary= "架子删除")
    @RequestMapping(value = "/shelfDelete", method = RequestMethod.POST)
    public CommonResult shelfDelete(@Parameter(name ="数据主键") @RequestParam(required = true) String shelfId) {
        try{

            iotShelfService.removeById(shelfId);
            return CommonResult.success();
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }



    @Operation(summary= "位置列表")
    @RequestMapping(value = "/locationList", method = RequestMethod.POST)
    public CommonResult locationList(@RequestBody JSONObject query) {
        try {
            TableList<JSONObject> list = iotHttpUtil.getList("/iot/easyfinder/location/list", query);

            return CommonResult.success(list);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }
    }


    @Operation(summary= "位置下拉")
    @RequestMapping(value = "/locationSelectList", method = RequestMethod.POST)
    public CommonResult locationSelectList(@RequestBody JSONObject query) {
        try {
            query.put("pageNum",1);
            query.put("pageSize",100);
            query.put("orderByColumn","create_time");
            query.put("isAsc","desc");
            TableList<JSONObject> list = iotHttpUtil.getList("/iot/easyfinder/location/list", query);

            return CommonResult.success(list.getList());
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }
    }

    @Operation(summary= "位置编辑")
    @RequestMapping(value = "/locationEdit", method = RequestMethod.POST)
    public CommonResult locationEdit(@RequestBody JSONObject object) {
        try {
            JSONObject jsonObject = iotHttpUtil.httpPut("/iot/easyfinder/location", object);
            return CommonResult.success(jsonObject);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }
    }

    @Operation(summary= "位置新增")
    @RequestMapping(value = "/locationAdd", method = RequestMethod.POST)
    public CommonResult locationAdd(@RequestBody JSONObject object) {
        try {
            JSONObject jsonObject = iotHttpUtil.httpPost("/iot/easyfinder/location", object);
            return CommonResult.success(jsonObject);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }
    }


    @Operation(summary= "位置删除")
    @RequestMapping(value = "/locationDelete", method = RequestMethod.POST)
    public CommonResult locationDelete(@RequestBody JSONObject object) {
        try {
            String string = object.getString("ids");
            if (UtilValidate.isEmpty(string)) {
                return CommonResult.error("参数有误!");
            }
            JSONObject jsonObject1 = iotHttpUtil.httpDelete("/iot/easyfinder/location/checkBeforeDelete/"+string, object);
            if(UtilValidate.isNotEmpty(jsonObject1)&&jsonObject1.getInteger("data")>0){
                return CommonResult.success("被占用："+jsonObject1.getInteger("data"));
            }
            JSONObject jsonObject = iotHttpUtil.httpDelete("/iot/easyfinder/location/"+string, object);
            return CommonResult.success(jsonObject);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }
    }



    @Operation(summary= "网关列表")
    @RequestMapping(value = "/gatewayList", method = RequestMethod.POST)
    public CommonResult gatewayList(@RequestBody JSONObject query) {
        try {
            TableList<JSONObject> list = iotHttpUtil.getList("/iot/easyfinder/localGateway/list", query);

            return CommonResult.success(list);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }
    }

    @Operation(summary= "网关编辑")
    @RequestMapping(value = "/gatewayEdit", method = RequestMethod.POST)
    public CommonResult gatewayEdit(@RequestBody JSONObject object) {
        try {
            JSONObject jsonObject = iotHttpUtil.httpPut("/iot/easyfinder/localGateway", object);
            return CommonResult.success(jsonObject);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }
    }

    @Operation(summary= "网关新增")
    @RequestMapping(value = "/gatewayAdd", method = RequestMethod.POST)
    public CommonResult gatewayAdd(@RequestBody JSONObject object) {
        try {
            JSONObject jsonObject = iotHttpUtil.httpPost("/iot/easyfinder/localGateway", object);
            return CommonResult.success(jsonObject);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }
    }


    @Operation(summary= "网关删除")
    @RequestMapping(value = "/gatewayDelete", method = RequestMethod.POST)
    public CommonResult gatewayDelete(@RequestBody JSONObject object) {
        try {
            String string = object.getString("ids");
            if (UtilValidate.isEmpty(string)) {
                return CommonResult.error("参数有误!");
            }
            JSONObject jsonObject = iotHttpUtil.httpDelete("/iot/easyfinder/localGateway/"+string, object);
            return CommonResult.success(jsonObject);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }
    }


    @Operation(summary= "网关-一键亮灯 查找坏标签")
    @RequestMapping(value = "/gatewayFindBadLabel", method = RequestMethod.POST)
    public CommonResult gatewayFindBadLabel(@RequestBody JSONObject object) {
        try {

            JSONObject jsonObject = iotHttpUtil.getDetail("/iot/easyfinder/localGateway/lightAll", object);
            return CommonResult.success(jsonObject);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }
    }

    /**
     * 模式-------开始
     * @param query
     * @return
     */
    @Operation(summary= "模式列表")
    @RequestMapping(value = "/modeList", method = RequestMethod.POST)
    public CommonResult modeList(@RequestBody JSONObject query) {
        try {
            TableList<JSONObject> list = iotHttpUtil.getList("/iot/easyfinder/bg01LightingMode/list", query);

            return CommonResult.success(list);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }
    }

    @Operation(summary= "模式编辑")
    @RequestMapping(value = "/modeEdit", method = RequestMethod.POST)
    public CommonResult modeEdit(@RequestBody JSONObject object) {
        try {
            JSONObject jsonObject = iotHttpUtil.httpPut("/iot/easyfinder/bg01LightingMode", object);
            return CommonResult.success(jsonObject);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }
    }

    @Operation(summary= "模式新增")
    @RequestMapping(value = "/modeAdd", method = RequestMethod.POST)
    public CommonResult modeAdd(@RequestBody JSONObject object) {
        try {
            JSONObject jsonObject = iotHttpUtil.httpPost("/iot/easyfinder/bg01LightingMode", object);
            return CommonResult.success(jsonObject);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }
    }


    @Operation(summary= "模式删除")
    @RequestMapping(value = "/modeDelete", method = RequestMethod.POST)
    public CommonResult modeDelete(@RequestBody JSONObject object) {
        try {
            String string = object.getString("ids");
            if (UtilValidate.isEmpty(string)) {
                return CommonResult.error("参数有误!");
            }
            JSONObject jsonObject = iotHttpUtil.httpDelete("/iot/easyfinder/bg01LightingMode/"+string, object);
            return CommonResult.success(jsonObject);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }
    }

    /**
     * 模式-------结束
     * @param query
     * @return
     */
    /**
     * 标签-------开始
     * @param query
     * @return
     */
    @Operation(summary= "标签列表")
    @RequestMapping(value = "/bgTagList", method = RequestMethod.POST)
    public CommonResult bgTagList(@RequestBody JSONObject query) {
        try {
            TableList<JSONObject> list = iotHttpUtil.getList("/iot/easyfinder/bgTag/list", query);

            return CommonResult.success(list);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }
    }

    @Operation(summary= "标签编辑")
    @RequestMapping(value = "/bgTagEdit", method = RequestMethod.POST)
    public CommonResult bgTagEdit(@RequestBody JSONObject object) {
        try {
            JSONObject jsonObject = iotHttpUtil.httpPut("/iot/easyfinder/bgTag", object);

            return CommonResult.success(jsonObject);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }
    }

    @Operation(summary= "标签新增")
    @RequestMapping(value = "/bgTagAdd", method = RequestMethod.POST)
    public CommonResult bgTagAdd(@RequestBody JSONObject object) {
        try {
            JSONObject jsonObject = iotHttpUtil.httpPost("/iot/easyfinder/bgTag", object);
            return CommonResult.success(jsonObject);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }
    }


    @Operation(summary= "标签删除")
    @RequestMapping(value = "/bgTagDelete", method = RequestMethod.POST)
    public CommonResult bgTagDelete(@RequestBody JSONObject object) {
        try {
            String string = object.getString("ids");
            if (UtilValidate.isEmpty(string)) {
                return CommonResult.error("参数有误!");
            }
            JSONObject jsonObject = iotHttpUtil.httpDelete("/iot/easyfinder/bgTag/"+string, object);
            return CommonResult.success(jsonObject);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }
    }


    @Operation(summary= "标签导入")
    @RequestMapping(value = "/bgTagImport", method = RequestMethod.POST)
    public CommonResult bgTagImport(@RequestBody List<JSONObject> list) {
        try {
//            JSONObject jsonObject = iotHttpUtil.httpPost("/iot/easyfinder/bgTag", object);
            iotTagService.bgTagImport(list);
            return CommonResult.success();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }
    }


    @Operation(summary= "日志列表")
    @RequestMapping(value = "/logList", method = RequestMethod.POST)
    public CommonResult logList(@RequestBody JSONObject query) {
        try {
            TableList<JSONObject> list = iotHttpUtil.getList("/iot/easyfinder/lightCmdLog/list", query);
            return CommonResult.success(list);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return CommonResult.error(e.getMessage());
        }
    }


}
